package com.shadow.bus.adapter;

import android.content.Context;

import com.chad.library.adapter.base.BaseSectionQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.shadow.bus.entity.TroubleOperationEntity;
import com.shadow.bus.main.work.TroubleSection;

import java.util.List;

import busfix.tepth.com.shadow_bus.R;

/**
 * @author zhangyu
 * @date 2018/7/5
 */

public class TroubleTypeAdapter extends BaseSectionQuickAdapter<TroubleSection, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param layoutResId      The layout resource id of each item.
     * @param sectionHeadResId The section head layout id for each item
     * @param data             A new list is created out of this one to avoid mutable list
     */
    private int mDrawableUnResole;
    private int mDrawableDone;

    public TroubleTypeAdapter(Context context,
                              int layoutResId,
                              int sectionHeadResId,
                              List<TroubleSection> data) {
        super(layoutResId, sectionHeadResId, data);
        mDrawableUnResole = R.drawable.bg_tag_normal;
        mDrawableDone = R.drawable.bg_tag_select;
    }

    @Override
    protected void convertHead(BaseViewHolder helper, TroubleSection item) {
        helper.setText(R.id.tv_header, item.header);
    }

    @Override
    protected void convert(BaseViewHolder helper, TroubleSection item) {
        List<TroubleOperationEntity> list = item.t.getListContent();
        if (list == null || list.isEmpty()) {
            helper.setBackgroundRes(R.id.tv_name, mDrawableUnResole);
        } else {
            boolean hasDone = false;
            for (TroubleOperationEntity entity : list) {
                if (entity.getWorkPerson() != null && !entity.isRework()) {
                    hasDone = true;
                    break;
                }
            }
            if (hasDone) {
                helper.setBackgroundRes(R.id.tv_name, mDrawableDone);
            } else {
                helper.setBackgroundRes(R.id.tv_name, mDrawableUnResole);
            }
        }
        helper.setText(R.id.tv_name, item.t.getName());
        helper.addOnClickListener(R.id.tv_name);
    }
}
